
package link.hwx.controller.oa;

import link.hwx.util.AjaxResult;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;


/**
 * 签名工具
 */

@RestController
@RequestMapping("/cx")
public class SignUtilOpen {


    public static final String HMAC_SHA256 = "HmacSHA256";
    public static final String ENCODING = "UTF-8";
    public static final String LF = "\n";
    public static final String SPE1 = ",";
    public static final String SPE2 = ":";
    public static final String SPE3 = "&";
    public static final String SPE4 = "=";
    public static final String SPE5 = "?";
    @PostMapping("/signUtilOpen")
    public static AjaxResult sign(@RequestBody Map<String, Object> datas) {
        String signature = "";
        String appsecret = String.valueOf(datas.get("appsecret"));
        Map<String,String>  querys = (Map<String, String>) datas.get("querys");
        AjaxResult resData = new AjaxResult();
        try {
            Mac hmacSha256 = Mac.getInstance(HMAC_SHA256);
            byte[] keyBytes = appsecret.getBytes(ENCODING);
            hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, HMAC_SHA256));
            signature = new String(Base64.encodeBase64(hmacSha256.doFinal(buildResource(querys).getBytes(ENCODING))),
                    ENCODING);
        } catch (Exception e) {
            resData.put("mes","获取失败");
            throw new RuntimeException(e);
        }

        resData.put("sign", signature);
        return resData;
    }

    private static String buildResource(Map<String, String> querys) {
        StringBuilder sb = new StringBuilder();
        Map<String, String> sortMap = new TreeMap<String, String>();
        if (null != querys) {
            for (Map.Entry<String, String> query : querys.entrySet()) {
                if (!StringUtils.isBlank(query.getKey())) {
                    sortMap.put(query.getKey(), query.getValue());
                }
            }
        }

        StringBuilder sbParam = new StringBuilder();
        for (Map.Entry<String, String> item : sortMap.entrySet()) {
            if (!StringUtils.isBlank(item.getKey())) {
                if (0 < sbParam.length()) {
                    sbParam.append(SPE3);
                }
                sbParam.append(item.getKey());
                if (!StringUtils.isBlank(item.getValue())) {
                    sbParam.append(SPE4).append(item.getValue());
                }
            }
        }
        if (0 < sbParam.length()) {
            sb.append(SPE5);
            sb.append(sbParam);
        }
        //System.out.println(">>>>>>>>>>>>>>>>>>>>");
        //System.out.println(sb.toString());
        //System.out.println(">>>>>>>>>>>>>>>>>>>>");
        return sb.toString();
    }
}
